package app.softtech.freechat.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHanler extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	public DatabaseHanler(Context context) {
		super(context, ConstantsDB.DATABASE_NAME, null,
				ConstantsDB.DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_DATABASE_USER);
		db.execSQL(CREATE_TABLE_DATABASE_FRIEND);
		db.execSQL(CREATE_TABLE_DATABASE_CHAT);
		db.execSQL(CREATE_TABLE_DATABASE_GROUP);
		db.execSQL(CREATE_TABLE_DATABASE_MEMBER_IN_GROUP);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		db.execSQL("DROP TABLE IF EXISTS " + ConstantsDB.DATABASE_USER);
		db.execSQL("DROP TABLE IF EXISTS " + ConstantsDB.DATABASE_USER);
		db.execSQL("DROP TABLE IF EXISTS " + ConstantsDB.DATABASE_CHAT);
		db.execSQL("DROP TABLE IF EXISTS " + ConstantsDB.DATABASE_GROUP);
		db.execSQL("DROP TABLE IF EXISTS " + ConstantsDB.DATABASE_MEMBER_IN_GROUP);
		onCreate(db);

	}

	// Table create Statement
	// DatabaseUser table create Statement
	private static final String CREATE_TABLE_DATABASE_USER = "CREATE TABLE "
			+ ConstantsDB.DATABASE_USER + "(" + ConstantsDB.USER_ID
			+ " INTEGER PRIMARY KEY, " + ConstantsDB.USERNAME
			+ " TEXT, " + ConstantsDB.PASSWORD + " TEXT, " + ConstantsDB.EMAIL
			+ " TEXT, " + ConstantsDB.PHONE + " TEXT, " + ConstantsDB.AVATAR
			+ " TEXT, " + ConstantsDB.IS_LOGGEDIN + " INTEGER, "
			+ ConstantsDB.STATUS + " TEXT " + ")";

	// DatabaseFriend table create statement
	private static final String CREATE_TABLE_DATABASE_FRIEND = "CREATE TABLE "
			+ ConstantsDB.DATABASE_FRIEND + "(" + ConstantsDB.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + ConstantsDB.USER_ID
			+ " INTEGER, " + ConstantsDB.FRIEND_ID + " INTEGER, "
			+ ConstantsDB.FRIEND_NAME + " TEXT, " + ConstantsDB.FRIEND_AVATAR
			+ " TEXT, " + ConstantsDB.FRIEND_STATUS + " TEXT, "
			+ ConstantsDB.FRIEND_PHONE + " TEXT, " + ConstantsDB.LAST_MESSAGE
			+ " TEXT, " + ConstantsDB.REQUEST_ID + " INTEGER " + ")";

	// DatabaseGroup table create statement
	private static final String CREATE_TABLE_DATABASE_GROUP = "CREATE TABLE "
			+ ConstantsDB.DATABASE_GROUP + "(" + ConstantsDB.GROUP_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + ConstantsDB.USER_ID
			+ " INTEGER, " + ConstantsDB.GROUP_NAME + " TEXT " + ")";

	// DatabaseMemberInServer table create

	private static final String CREATE_TABLE_DATABASE_MEMBER_IN_GROUP = "CREATE TABLE "
			+ ConstantsDB.DATABASE_MEMBER_IN_GROUP
			+ "("
			+ ConstantsDB.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ ConstantsDB.GROUP_ID
			+ " INTEGER, "
			+ ConstantsDB.USER_ID_IN_SERVER
			+ " INTEGER, "
			+ ConstantsDB.IS_IN_FRIEND_LIST
			+ " INTEGER, "
			+ ConstantsDB.USER_NAME_IN_SERVER + " TEXT" + ")";

	// DatabaseChat table table create statement
	private static final String CREATE_TABLE_DATABASE_CHAT = "CREATE TABLE "
			+ ConstantsDB.DATABASE_CHAT + " ( " + ConstantsDB.ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + ConstantsDB.MESSAGE
			+ " TEXT, " + ConstantsDB.ISSEND + " INTEGER, "
			+ ConstantsDB.USER_ID + " INTEGER, " + ConstantsDB.FRIEND_ID
			+ " INTEGER, " + ConstantsDB.GROUP_ID + " INTEGER, "
			+ ConstantsDB.DATECHAT + " DATETIME " + ")";

	/**
	 * insert row data into DatabaseUser
	 * 
	 * @param databaseUser
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertDatabseUser(DatabaseUser databaseUser) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		if (checkUserName(databaseUser.getUserName()) == -1) {
			values = databaseUser.getValuesUser();
			// Inserts row
			long user_id = db.insert(ConstantsDB.DATABASE_USER, null, values);
			return user_id;
		}
		return -1;
	}

	/**
	 * Check username exits in DatabaseUser Table
	 * 
	 * @param username
	 * @return 1 if exits, -1 if not exits :)
	 */
	public int checkUserName(String username) {
		List<DatabaseUser> listUser = new ArrayList<DatabaseUser>();
		listUser = this.getAllDatabaseUser();
		for (DatabaseUser us : listUser) {
			if (us.getUserName().equals(username)) {
				return 1;
			}
		}
		return -1;
	}

	/**
	 * Get single a DatabaseUser by User_id
	 * 
	 * @param user_id
	 * @return a DatabaseUser
	 */
	public DatabaseUser getDatabaseUser(int user_id) {
		SQLiteDatabase db = this.getReadableDatabase();

		String selectQuery = "SELECT * FROM " + ConstantsDB.DATABASE_USER
				+ " WHERE " + ConstantsDB.USER_ID + " = " + user_id;
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor != null){
			cursor.moveToFirst();
			DatabaseUser user = new DatabaseUser();
			user = DatabaseUser.setValuesFromCursor(cursor);
			return user;
		}
		else return null;	
		
	}

	/**
	 * Get all row data in DatabaseUser
	 * 
	 * @return List DatabaseUser
	 */
	public List<DatabaseUser> getAllDatabaseUser() {

		List<DatabaseUser> listUser = new ArrayList<DatabaseUser>();
		SQLiteDatabase db = this.getReadableDatabase();

		String selectQuery = "SELECT * FROM " + ConstantsDB.DATABASE_USER;
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				DatabaseUser user = new DatabaseUser();
				user = DatabaseUser.setValuesFromCursor(cursor);
				listUser.add(user);

			} while (cursor.moveToNext());
		}

		return listUser;
	}

	/**
	 * Get all Friend of user has username in param
	 * 
	 * @param userName
	 * @return List DatabaseFriend
	 */
	public List<DatabaseFriend> getAllFriendByUserName(String userName) {

		List<DatabaseFriend> listFriend = new ArrayList<DatabaseFriend>();
		SQLiteDatabase db = this.getReadableDatabase();

		String selectQuery = "SELECT * FROM " + ConstantsDB.DATABASE_FRIEND
				+ " friend, " + ConstantsDB.DATABASE_USER
				+ " user WHERE friend." + ConstantsDB.USER_ID + " = "
				+ " user." + ConstantsDB.USER_ID + " AND user."
				+ ConstantsDB.USERNAME + " = '" + userName + "' AND friend." + ConstantsDB.REQUEST_ID + " = " + 0 ;

		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
			do {
				DatabaseFriend friend = new DatabaseFriend();
				friend = DatabaseFriend.setValuesFromCursor(cursor);
				listFriend.add(friend);
			} while (cursor.moveToNext());
		}
		return listFriend;
	}

	/**
	 * get All Friend of single User by user_id
	 * 
	 * @param user
	 * @return
	 */

	public List<DatabaseFriend> getAllFriendByUserId(int user_id) {

		List<DatabaseFriend> listFriend = new ArrayList<DatabaseFriend>();
		SQLiteDatabase db = this.getReadableDatabase();

		String selectQuery = "SELECT * FROM " + ConstantsDB.DATABASE_FRIEND
				+ " friend, " + ConstantsDB.DATABASE_USER
				+ " user WHERE friend." + ConstantsDB.USER_ID + " = "
				+ " user." + ConstantsDB.USER_ID + " AND user."
				+ ConstantsDB.USER_ID + " = " + user_id + " AND friend." + ConstantsDB.REQUEST_ID + " = " + 0;

		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
			do {
				DatabaseFriend friend = new DatabaseFriend();
				friend = DatabaseFriend.setValuesFromCursor(cursor);
				listFriend.add(friend);
			} while (cursor.moveToNext());
		}
		return listFriend;

	}

	/**
	 * Update DatabaseUser
	 * 
	 * @param user
	 * @return row of affect
	 */
	public int updateDatabaseUser(DatabaseUser user) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values = user.getValuesUser();

		// updating Row
		return db.update(ConstantsDB.DATABASE_USER, values, ConstantsDB.USER_ID
				+ " = ?", new String[] { String.valueOf(user.getUserId()) });

	}

	/**
	 * Delete rows in table
	 * @param tableName
	 */
	public int  deleteTable(String tableName){
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete(tableName, null, null);
	}
	/**
	 * Delete DatabaseUser if want delete all friend of this user then set
	 * should_delete_all_friend = true else false
	 * 
	 * @param user
	 * @param should_delete_all_friend
	 */
	public void deleteDatabaseUser(int user_id,
			boolean should_delete_all_friend) {
		SQLiteDatabase db = this.getWritableDatabase();

		// Before deleting user
		// checks if friend of user should also be deleted
		// ( xoa luon friend cua thang user )

		if (should_delete_all_friend) {
			// get all friend of this user
			List<DatabaseFriend> listFriend = getAllFriendByUserId(user_id);

			for (DatabaseFriend f : listFriend) {
				deleteDatabaseFriend(f.getFriend_id());
			}
		}
		db.delete(ConstantsDB.DATABASE_USER, ConstantsDB.USER_ID + " = ?",
				new String[] { String.valueOf(user_id) });
	}

	/************ CRUD DatabaseFriend ******/

	/**
	 * insert a new friend
	 * 
	 * @param friend
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertDatabaseFriend(DatabaseFriend friend) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = friend.getValuesFriend();
		long id = db.insert(ConstantsDB.DATABASE_FRIEND, null, values);
		return id;
	}

	/**
	 * Check Friend_id of User
	 * 
	 * @param user_id
	 * @param friend_id
	 * @return if exits return 1 else return -1
	 */
	public int checkFriendOfUser(int user_id, int friend_id) {
		List<DatabaseFriend> listFriend = new ArrayList<DatabaseFriend>();
		listFriend = this.getAllFriend();
		for (DatabaseFriend f : listFriend) {
			if ((f.getFriend_id() == friend_id) && (f.getUser_id() == user_id))
				return 1;
		}
		return -1;
	}

	/**
	 * Updating a DatabaseFriend by ID
	 * 
	 * @param friend
	 * @return number row affect
	 */
	public int updateDatabaseFriend(DatabaseFriend friend) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values = friend.getValuesFriend();

		return db.update(ConstantsDB.DATABASE_FRIEND, values, ConstantsDB.ID
				+ " = ?", new String[] { String.valueOf(friend.getId())});
	}
	
	/**
	 * update friend by friend_id
	 * @param friend, friend_id
	 * @param friend_id
	 * @return
	 */
	public void updateDatabaseFriend(int friend_id){
		SQLiteDatabase db = this.getWritableDatabase();
		String sql = "UPDATE " + ConstantsDB.DATABASE_FRIEND + " SET " +
				ConstantsDB.REQUEST_ID + " = " + 0 + 
				" WHERE " + ConstantsDB.FRIEND_ID + " = " + friend_id;
		db.execSQL(sql);
	}

	/**
	 * Delete a DatabaseFriend by Id
	 * 
	 * @param int: id
	 * @return number row affect
	 */

	public int deleteDatabaseFriend(int id) {
		SQLiteDatabase db = this.getWritableDatabase();

		return db.delete(ConstantsDB.DATABASE_FRIEND, ConstantsDB.ID + " = ?",
				new String[] { String.valueOf(id) });
	}

	/**
	 * Delete friend by request id
	 * @param requestId
	 * @return
	 */
	public int deleteFriendByRequestId(int requestId){
		SQLiteDatabase db = this.getWritableDatabase();

		return db.delete(ConstantsDB.DATABASE_FRIEND, ConstantsDB.REQUEST_ID+ " = ?",
				new String[] { String.valueOf(requestId) });
	}
	/**
	 * Get All DatabaseFriend in DatabaseFriend table
	 * 
	 * @return
	 */
	public List<DatabaseFriend> getAllFriend() {
		List<DatabaseFriend> listFriend = new ArrayList<DatabaseFriend>();
		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_FRIEND;
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			do {
				DatabaseFriend friend = new DatabaseFriend();
				friend = DatabaseFriend.setValuesFromCursor(cursor);
				listFriend.add(friend);

			} while (cursor.moveToNext());
		}
		return listFriend;
	}
	
	public DatabaseFriend getFriendByRequestId(String requestId){
		
		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_FRIEND + 
				" WHERE " + ConstantsDB.REQUEST_ID + " = " + requestId;		
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		DatabaseFriend friend;
		if(cursor.moveToFirst()){
			friend = DatabaseFriend.setValuesFromCursor(cursor);
			return friend;
		}
		else 
			return null;
	}
	
	public List<DatabaseFriend> getAllFriendByRequestId(String[] request){
		List<DatabaseFriend> listFriend = new ArrayList<DatabaseFriend>();	
		int length = request.length;
		int i;
		for(i= 0; i < length ; i++){
			DatabaseFriend f = new DatabaseFriend();
			if(getFriendByRequestId(request[i])!=null){
				f = getFriendByRequestId(request[i]);
				listFriend.add(f);
			}					
		}
		return listFriend;
	}
	
	public List<DatabaseFriend> getAllFriendRequest(){
		List<DatabaseFriend> listFriend = new ArrayList<DatabaseFriend>();	
		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_FRIEND + 
				" WHERE " + ConstantsDB.REQUEST_ID + " <> " + 0;		
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.moveToFirst()){
			do {
				DatabaseFriend f = new DatabaseFriend();
				f = DatabaseFriend.setValuesFromCursor(cursor);
				listFriend.add(f);
			} while (cursor.moveToNext());			
		}
		return listFriend;
	}

	/************** CRUD DAtabaseGroup table */

	// Insert a new DatabaseGroup

	public long insertDatabaseGroup(DatabaseGroup dbGroup) {

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values = dbGroup.getValuesDatabaseGroup();
		values.remove(ConstantsDB.ID);

		return db.insert(ConstantsDB.DATABASE_GROUP, null, values);
	}

	/**
	 * update a databaseGroup
	 * 
	 * @param dbGroup
	 * @return number of row affect
	 */
	public long updateDatabaseGroup(DatabaseGroup dbGroup) {

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values = dbGroup.getValuesDatabaseGroup();
		values.remove(ConstantsDB.GROUP_ID);

		return db.update(ConstantsDB.DATABASE_GROUP, values,
				ConstantsDB.GROUP_ID + " = ? ",
				new String[] { String.valueOf(dbGroup.getGroupId()) });
	}

	/**
	 * 
	 * @param databaseGroup
	 * @return number of rows affect
	 */
	public int deleteDatabaseGroup(DatabaseGroup g) {

		SQLiteDatabase db = this.getWritableDatabase();

		return db.delete(ConstantsDB.DATABASE_GROUP, ConstantsDB.GROUP_ID
				+ " = ? ", new String[] { String.valueOf(g.getGroupId()) });
	}

	/**
	 * Get All rows data in DatabaseGroup table
	 * 
	 * @return
	 */
	public List<DatabaseGroup> getAllDatabaseGroup() {

		SQLiteDatabase db = this.getReadableDatabase();

		String selectQuery = "SELECT * FROM " + ConstantsDB.DATABASE_GROUP;

		Cursor c = db.rawQuery(selectQuery, null);

		List<DatabaseGroup> listGroup = new ArrayList<DatabaseGroup>();

		if (c.moveToFirst()) {
			do {
				DatabaseGroup dg = new DatabaseGroup();
				dg.setGroupId(c.getInt(c.getColumnIndex(ConstantsDB.GROUP_ID)));
				dg.setUserId(c.getInt(c.getColumnIndex(ConstantsDB.USER_ID)));
				dg.setGroupName(c.getString(c
						.getColumnIndex(ConstantsDB.GROUP_NAME)));
				listGroup.add(dg);
			} while (c.moveToNext());
		}

		return listGroup;
	}

	public List<DatabaseGroup> getAllDatabaseGroupByUserId(int user_id) {
		SQLiteDatabase db = this.getReadableDatabase();

		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_GROUP + " WHERE "
				+ ConstantsDB.USER_ID + " = " + user_id;

		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(user_id) });

		List<DatabaseGroup> listGroup = new ArrayList<DatabaseGroup>();

		if (c.moveToFirst()) {
			do {
				DatabaseGroup dg = new DatabaseGroup();
				dg.setGroupId(c.getInt(c.getColumnIndex(ConstantsDB.GROUP_ID)));
				dg.setUserId(c.getInt(c.getColumnIndex(ConstantsDB.USER_ID)));
				dg.setGroupName(c.getString(c
						.getColumnIndex(ConstantsDB.GROUP_NAME)));
				listGroup.add(dg);
			} while (c.moveToNext());
		}

		return listGroup;
	}

	/**
	 * Get DatabaseGroup has group_id specialize in DatabaseGroup Table
	 * 
	 * @param DatabaseGroup
	 * @return a DatabaseGroup
	 */

	public DatabaseGroup getDatabaseGroup(int group_id) {

		SQLiteDatabase db = this.getReadableDatabase();
		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_GROUP + " WHERE "
				+ ConstantsDB.GROUP_ID + " = " + group_id;

		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(group_id) });

		DatabaseGroup dg = new DatabaseGroup();

		if (c.moveToFirst()) {
			dg.setGroupId(c.getInt(c.getColumnIndex(ConstantsDB.GROUP_ID)));
			dg.setUserId(c.getInt(c.getColumnIndex(ConstantsDB.USER_ID)));
			dg.setGroupName(c.getString(c
					.getColumnIndex(ConstantsDB.GROUP_NAME)));

		}
		return dg;

	}

	/**
	 * Insert MemberInGroup into DatabaseMemberInGroup table
	 */

	// Insert into DatabaseMemberInGroup table
	public long insertMemberInGroup(MemberInGroup member) {

		ContentValues values = new ContentValues();
		values = member.getValuesMember();

		SQLiteDatabase db = this.getWritableDatabase();
		long id;
		try {
			id = db.insert(ConstantsDB.DATABASE_MEMBER_IN_GROUP, null, values);
			return id;
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}

	}

	/**
	 * Update MemberInGrouop
	 * 
	 * @param memberInGroup
	 * @return number Rows affect
	 */
	public long updateMemberInGroup(MemberInGroup member) {
		ContentValues values = new ContentValues();
		values = member.getValuesMember();

		SQLiteDatabase db = this.getWritableDatabase();

		return db.update(ConstantsDB.DATABASE_MEMBER_IN_GROUP, values,
				ConstantsDB.ID + " = ?",
				new String[] { String.valueOf(member.getGroupId()) });
	}

	/**
	 * Delete MemberInGroup
	 * 
	 * @param id
	 * @return number of row affect
	 */
	public int deleteMemberInGroup(int id) {
		SQLiteDatabase db = this.getWritableDatabase();

		return db.delete(ConstantsDB.DATABASE_MEMBER_IN_GROUP, ConstantsDB.ID
				+ " = ? ", new String[] { String.valueOf(id) });

	}

	/**
	 * Get All DatabaseMemberInGroup in it's table
	 * 
	 * @return List<MemberInGroup> listMember
	 */
	public List<MemberInGroup> getAllMemberInGroup() {

		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_MEMBER_IN_GROUP;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);

		List<MemberInGroup> listMember = new ArrayList<MemberInGroup>();
		if (cursor.moveToFirst()) {
			do {
				MemberInGroup member = new MemberInGroup();
				member.setId(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.ID)));
				member.setGroupId(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.GROUP_ID)));
				member.setUserIdInServer(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.USER_ID_IN_SERVER)));
				member.setIsInFriendList(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.IS_IN_FRIEND_LIST)));
				member.setUserNameInSerVer(cursor.getString(cursor
						.getColumnIndex(ConstantsDB.USER_NAME_IN_SERVER)));
				listMember.add(member);
			} while (cursor.moveToNext());
		}
		return listMember;
	}

	/**
	 * Get MEmberInGroup if has id = parram
	 * 
	 * @param id
	 * @return MemberInGroup
	 */
	public MemberInGroup getMemberInGroupsById(int id) {
		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_MEMBER_IN_GROUP
				+ " WHERE " + ConstantsDB.ID + " = " + id;
		SQLiteDatabase db = this.getReadableDatabase();
		MemberInGroup member = new MemberInGroup();
		Cursor cursor = db.rawQuery(sql, new String[] { String.valueOf(id) });
		if (cursor.moveToFirst()) {
			do {
				member.setId(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.ID)));
				member.setGroupId(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.GROUP_ID)));
				member.setUserIdInServer(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.USER_ID_IN_SERVER)));
				member.setIsInFriendList(cursor.getInt(cursor
						.getColumnIndex(ConstantsDB.IS_IN_FRIEND_LIST)));
				member.setUserNameInSerVer(cursor.getString(cursor
						.getColumnIndex(ConstantsDB.USER_NAME_IN_SERVER)));
			} while (cursor.moveToNext());
		}
		return member;
	}

	/**
	 * Insert row into DatabaseChat
	 * 
	 * @param chat
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insertDatabaseChat(DatabaseChat chat) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values = chat.getValuesChat();
		return db.insert(ConstantsDB.DATABASE_CHAT, null, values);
	}

	/**
	 * Update row data in DatabaseChat table
	 * 
	 * @param Databasechat
	 * @return the number of rows affected
	 */
	public int updateDatabaseChat(DatabaseChat chat) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values = chat.getValuesChat();
		return db.update(ConstantsDB.DATABASE_CHAT, values, ConstantsDB.ID
				+ " = ? ", new String[] { String.valueOf(chat.getId()) });
	}

	/**
	 * Delete DatabaseChat by Id
	 * 
	 * @param id
	 * @return number rows affected
	 */
	public int deleteChatById(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete(ConstantsDB.DATABASE_CHAT, ConstantsDB.ID + " = ? ",
				new String[] { String.valueOf(id) });

	}

	/**
	 * Delete DatabaseChat by group_id
	 * 
	 * @param group_id
	 * @return number of rows affected
	 */
	public int deleteChatByGroupId(int group_id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete(ConstantsDB.DATABASE_CHAT, ConstantsDB.GROUP_ID
				+ " = ? ", new String[] { String.valueOf(group_id) });
	}

	/**
	 * Delete DatabaseChat by FriendId
	 * 
	 * @param friend_id
	 * @return
	 */
	public int deleteChatByFriendId(int friend_id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete(ConstantsDB.DATABASE_CHAT, ConstantsDB.FRIEND_ID
				+ " = ? ", new String[] { String.valueOf(friend_id) });
	}

	/**
	 * Get All DatabaseChat of a single user
	 * 
	 * @return List DatabaseChat
	 */
	public List<DatabaseChat> getAllDatabaseChatOfUser(int user_id) {
		List<DatabaseChat> listDataChat = new ArrayList<DatabaseChat>();
		SQLiteDatabase db = this.getReadableDatabase();
		String sql = "SELECT * FROM " + ConstantsDB.DATABASE_CHAT + " WHERE "
				+ ConstantsDB.USER_ID + " = " + user_id;
		Cursor cursor = db.rawQuery(sql,
				new String[] { String.valueOf(user_id) });
		if (cursor.moveToFirst()) {
			do {
				DatabaseChat dataChat = new DatabaseChat();
				dataChat = DatabaseChat.setValuesFromCursor(cursor);
				listDataChat.add(dataChat);
			} while (cursor.moveToNext());
		}
		return listDataChat;
	}

	/**
	 * Get All DatabaseChat by group_id
	 * 
	 * @param group_id
	 * @return
	 */
	public List<DatabaseChat> getAllDatabaseChatByGroup(int user_id,
			int group_id) {
		List<DatabaseChat> listDataChat = new ArrayList<DatabaseChat>();
		SQLiteDatabase db = this.getReadableDatabase();
		String sql = "SELECT TOP 60 * FROM " + ConstantsDB.DATABASE_CHAT
				+ " chat " + " WHERE chat." + ConstantsDB.GROUP_ID + " = "
				+ group_id + " AND " + " chat." + ConstantsDB.USER_ID + " = "
				+ user_id;
		Cursor cursor = db.rawQuery(
				sql,
				new String[] { String.valueOf(group_id),
						String.valueOf(user_id) });
		if (cursor.moveToFirst()) {
			do {
				DatabaseChat dataChat = new DatabaseChat();
				dataChat = DatabaseChat.setValuesFromCursor(cursor);
				listDataChat.add(dataChat);
			} while (cursor.moveToNext());
		}
		return listDataChat;
	}

	/**
	 * Get All DatabaseChat by friend_id and userid
	 * 
	 * @param group_id
	 * @return
	 */
	public List<DatabaseChat> getAllDatabaseChatByFriend(int user_id,
			int friend_id) {
		List<DatabaseChat> listDataChat = new ArrayList<DatabaseChat>();
		SQLiteDatabase db = this.getReadableDatabase();
		String sql = "SELECT TOP 60 * FROM " + ConstantsDB.DATABASE_CHAT
				+ " chat " + " WHERE chat." + ConstantsDB.FRIEND_ID + " = "
				+ friend_id + " AND " + " chat." + ConstantsDB.USER_ID + " = "
				+ user_id;
		Cursor cursor = db.rawQuery(
				sql,
				new String[] { String.valueOf(friend_id),
						String.valueOf(user_id) });
		if (cursor.moveToFirst()) {
			do {
				DatabaseChat dataChat = new DatabaseChat();
				dataChat = DatabaseChat.setValuesFromCursor(cursor);
				listDataChat.add(dataChat);
			} while (cursor.moveToNext());
		}
		return listDataChat;
	}

	/******** Closing Database ************/

	public void closeDB() {

		SQLiteDatabase db = this.getReadableDatabase();
		if (db != null && db.isOpen()) {
			db.close();
		}
	}
}
